package com.sgcc.pda.sdk.utils;


import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


/**
 * @author WangJinKe
 * @version: 1.0
 * @CreateTime 2015年4月1日 下午2:45:51
 * @Description:时间处理
 */
@SuppressLint("SimpleDateFormat")
public class DateUtil {
    private static String pattern1 = "yy-MM-dd";
    private static String pattern2 = "yyyy-MM-dd";
    private static String pattern3 = "yyyy-MM-dd HH:mm:ss";
    private static String pattern4 = "yy-MM-dd HH:mm:ss";
    private static SimpleDateFormat time8 = new SimpleDateFormat(pattern1);

    private DateUtil() {
    }

    static SimpleDateFormat dateformat;

    public static class DatePattern {
        public static final String YMD_HMS = pattern3;
        public static final String HM = "HH:mm";
    }

    /**
     * 获取时分秒
     *
     * @return
     */
    public static String getHourTime() {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        String time = format.format(new Date());
        return time;
    }

    /**
     * 获取当前时间 pattern3
     *
     * @return
     */
    public static String getTime() {
        SimpleDateFormat format = new SimpleDateFormat(pattern3);
        String time = format.format(new Date());
        return time;
    }

    public static String getTime1() {
        SimpleDateFormat format = new SimpleDateFormat(pattern4);
        String time = format.format(new Date());
        return time;
    }


    public static String getDate() {
        Calendar calendar = Calendar.getInstance();
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
        String time = format.format(new Date()) + "0" + week;
        return time;
    }

    /**
     * 获取当前时间 pattern3
     *
     * @return
     */
    public static String getTime(String str) {
        SimpleDateFormat format = new SimpleDateFormat(pattern3);
        String time = format.format(str);
        return time;
    }

    public static String get2YTime(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HHmmss");
        try {
            Date meterTime = format.parse(str);
            SimpleDateFormat format1 = new SimpleDateFormat(pattern4);
            String time = format1.format(meterTime);
            return time;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "";
    }

    /**
     * 获取时间  pattern4
     *
     * @return
     */
    public static String get2YTime() {
        SimpleDateFormat format = new SimpleDateFormat(pattern4);
        String time = format.format(new Date());
        return time;
    }

    /**
     * 获取时间  yyyyMMddHHmmss
     *
     * @return
     */
    public static String get3YTime() {
        //SimpleDateFormat format = new SimpleDateFormat(pattern4);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = format.format(new Date());
        return time;
    }

    /**
     * 获取时间  pattern1
     *
     * @return
     */
    public static String get2YDTime() {
        SimpleDateFormat format = new SimpleDateFormat(pattern1);
        String time = format.format(new Date());
        return time;
    }

    /**
     * 得到前n天的日期
     *
     * @return
     */
    public static String getOneDayTime(int index) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, -index);
        return time8.format(cal.getTime());


//        SimpleDateFormat sdf = new SimpleDateFormat(pattern1, Locale.getDefault());
//        Date d1 = sdf.parse(get2YDTime(), new ParsePosition(0));
//
//        long m1 = d1.getTime();
//        long m2 = m1 - (long) (index * (1000 * 3600 * 24));
//        Date d2 = new Date(m2);
//        return sdf.format(d2);
    }

    /* @param date1 <String>
     * @param date2 <String>
     * @return int
     * @throws ParseException
     */
    public int getDateSpace(String date1, String date2)
            throws ParseException {

        int result = 0;

        Calendar calst = Calendar.getInstance();

        Calendar caled = Calendar.getInstance();

        calst.setTime(time8.parse(date1));
        caled.setTime(time8.parse(date2));

        //设置时间为0时
        calst.set(Calendar.HOUR_OF_DAY, 0);
        calst.set(Calendar.MINUTE, 0);
        calst.set(Calendar.SECOND, 0);
        caled.set(Calendar.HOUR_OF_DAY, 0);
        caled.set(Calendar.MINUTE, 0);
        caled.set(Calendar.SECOND, 0);
        //得到两个日期相差的天数
        int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;

        return days;
    }

    public static String longToString(long currentTime) {
        Date date = new Date(currentTime);
        SimpleDateFormat format = new SimpleDateFormat(pattern3);
        return format.format(date);
    }

    /**
     * 得到前n天的日期
     *
     * @return
     */
    public static String getOneDayTime(String dataDate, int index) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time8.parse(dataDate, new ParsePosition(0)));
        cal.add(Calendar.DATE, -index);

        return time8.format(cal.getTime());

//        SimpleDateFormat sdf = new SimpleDateFormat(pattern1, Locale.getDefault());
//        Date d1 = sdf.parse(dataDate, new ParsePosition(0));
//
//        long m1 = d1.getTime();
//        long m2 = m1 - (long) (index * (1000 * 3600 * 24));
//        Date d2 = new Date(m2);
//        return sdf.format(d2);
    }


    /**
     * 获取某个时间的后一天
     *
     * @param date1 某个时间值  pattern1
     * @param index 天数
     * @return
     */
    public static String getLastOneDayTime(String date1, int index) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern1, Locale.getDefault());
        Date d1 = sdf.parse(date1, new ParsePosition(0));
        long m1 = d1.getTime();
        long m2 = m1 + (long) ((index - 1) * (1000 * 3600 * 24));
        Date d2 = new Date(m2);
        return sdf.format(d2);

    }


    /**
     * 获取当前时间 pattern2
     *
     * @return
     */
    public static String getToday() {
        SimpleDateFormat format = new SimpleDateFormat(pattern2);
        String now = format.format(new Date());
        return now;
    }

    /**
     * 获取当前时间pattern3
     *
     * @return
     */
    public static String getTodayDateTime() {
        SimpleDateFormat format = new SimpleDateFormat(pattern3);
        return format.format(new Date());
    }

    /**
     * 获取当前时间戳
     *
     * @return
     */
    public static long getDataStampNow() {
        SimpleDateFormat format = new SimpleDateFormat(pattern3);
        try {
            Date date = format.parse(format.format(new Date()));
            return date.getTime() / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


    /*
     * 将时间转换为时间戳
     */
    public long dateToStamp(String time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern3);
        Date date = simpleDateFormat.parse(time);
        return date.getTime();
    }

    /**
     * 获取X天前的时间
     *
     * @return
     */
    public static String getXDayAgo(int x) {
        Calendar c = Calendar.getInstance();
        int today = c.get(Calendar.DAY_OF_MONTH);
        c.set(Calendar.DATE, today - x);
        SimpleDateFormat format = new SimpleDateFormat(pattern2);
        String xDayAgo = format.format(c.getTime());
        return xDayAgo;
    }

    public static SimpleDateFormat getDateFormat() {
        if (dateformat == null) {
            dateformat = new SimpleDateFormat(pattern3);
        }
        return dateformat;
    }

    /**
     * 获取当前的时间,精确到小时
     */
    public static String getNowTime() {
        SimpleDateFormat format = new SimpleDateFormat(pattern3);
        return format.format(new Date());
    }

    /**
     * 获取当前的时间,精确到小时
     */
    public static String getNowTime(String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(new Date());
    }

    /**
     * 获取x小时前的时间
     *
     * @param x
     * @return
     */
    public static String getXHourAgo(int x) {
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        c.set(Calendar.HOUR_OF_DAY, hour - x);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
        String xHourAgo = format.format(c.getTime());
        return xHourAgo;
    }

    /**
     * 字符串时间 转成 Date类型
     *
     * @param str
     * @param pattern
     * @return
     */
    public static Date parse(String str, String pattern) {
        Date date = null;
        try {
            if (str == null || str.trim().length() == 0) {
                return null;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            date = sdf.parse(str);
        } catch (ParseException e) {
            System.out.println("DateUtils" + e.getMessage());
        }
        return date;
    }

    /**
     * 计算时间差
     * 不满一小时输出 X分钟
     * 满一小时，不满一天输出 X小时
     * 满一天 输出 X天
     *
     * @param date1 开始时间
     * @param date2 结束时间
     */

    public static String getDateDifferent(String date1, String date2) {
        SimpleDateFormat format = new SimpleDateFormat(pattern3);

        Date d1 = null;
        Date d2 = null;
        int time = 0;
        try {
            d1 = format.parse(date1);
            d2 = format.parse(date2);
            long diff = d2.getTime() - d1.getTime();//这样得到的差值是微秒级别
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            if (days < 1 && hours < 1) {
                time = (int) minutes;
                return time + "分钟前";
            } else if (days < 1 && hours >= 1) {
                time = (int) hours;
                return time + "小时前";
            } else if (days >= 1) {
                time = (int) days;
                return time + "天前";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static Date stringToDate(String dateString) {
        ParsePosition position = new ParsePosition(0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern3);
        Date dateValue = simpleDateFormat.parse(dateString, position);
        return dateValue;
    }

    /**
     * 获取某个日期的下一天 pattern3
     *
     * @param date
     * @return
     */
    public static String getNextDay(Date date) {
        date = date == null ? new Date() : date;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, day + 1);
        return new SimpleDateFormat(pattern3).format(calendar.getTime());
    }

    /**
     * 判断两个时间是否相隔一天
     */
    public static boolean overOneDay(String date1, String date2) {
        Date startTime = stringToDate(date1);
        Date endTime = stringToDate(date2);
        long between = endTime.getTime() - startTime.getTime();
        if (between > (24 * 3600000)) {
            return true;
        }
        return false;
    }

    /**
     * 当前时间的毫秒数转换为格式化时间
     */
    public static String currentTimeFormat() {
        long time = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern3);
        Date date = new Date();
        date.setTime(time);
        return sdf.format(date);
    }

    /**
     * 获取当前时间pattern3
     *
     * @return
     */
    public static String getRFIDTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date());
    }
    /**
     * 是否是今天
     *
     * @param timeLong 时间戳
     */
    public static boolean isToday(long timeLong) {
        Calendar calendar = Calendar.getInstance();
        // 设置今天起始时间
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        long start = calendar.getTime().getTime();
        // 设置今天结束时间
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        long end = calendar.getTime().getTime();
        // 大于等于起始时间，并小于等于结束时间
        return timeLong >= start && timeLong <= end;
    }

    /**
     * 是否是昨天
     *
     * @param timeLong 时间戳
     * @return
     */
    public static boolean isYesterday(long timeLong) {
        Calendar calendar = Calendar.getInstance();
        // 设置昨天起始时间
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        long start = calendar.getTime().getTime();
        // 设置昨天结束时间
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        long end = calendar.getTime().getTime();
        // 大于等于起始时间，并小于等于结束时间
        return timeLong > start && timeLong < end;
    }

    /**
     * 展示时间
     *
     * @param timeLong 时间戳
     * @return 如 ："下午 hh:mm"，"昨天 HH:mm"
     */
    public static String showTime(long timeLong) {
        String pattern;
        Date date = new Date(timeLong);
        if (isToday(timeLong)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            if (hour > 17) {
                pattern = "晚上 hh:mm";
            } else if (hour >= 0 && hour <= 6) {
                pattern = "凌晨 hh:mm";
            } else if (hour > 11 && hour <= 17) {
                pattern = "下午 hh:mm";
            } else {
                pattern = "上午 hh:mm";
            }
        } else if (isYesterday(timeLong)) {
            pattern = "昨天 HH:mm";
        } else {
            pattern = "M月d日 HH:mm";
        }
        return new SimpleDateFormat(pattern, Locale.getDefault()).format(new Date(timeLong));
    }
}
